<?php

namespace App\Http\Controllers;

use App\Machine;
use App\Workshop;
use Illuminate\Http\Request;



class WorkshopController extends Controller
{
    
     public function getAll() {
       $workshop=Workshop::all();
        return response()->json($workshop);
    }
    public function getCurrent($page) {
        $pages=$page*20-20;
        $workshop=Workshop::skip($pages)->take(20)->get();
        return response()->json($workshop);
    }
    public function one($id) {
        $workshop=Workshop::find($id);
        return response()->json($workshop);
    }

    public function getCount() {
        $workshop=Workshop::all()->count();
        return response()->json($workshop);
    }
    public function destroy($id)
    {
        Workshop::destroy($id);
        return response()->json('删除成功');
    }



    public function search($word,$type)
    {
        $workshop=Workshop::where($type,$word)->get();
        return response()->json($workshop);
    }

    public function add(Request $request)
    {
        $addWorkshop=$request->except('_token');
//        $addWorkshop['machineId']=$request->input('machineId');
//        $machine=Machine::find($addWorkshop['machineId']);
//        $addWorkshop['machineName']=$machine->machineName;
        Workshop::create($addWorkshop);
        return response()->json('添加成功');
    }



    public function update(Request $request, $id)
    {
        $updateWorkshop=$request->except('_token');
//        $updateWorkshop['machineId']=$request->input('machineId');
//        $machine=Machine::find($updateWorkshop['machineId']);
//        $updateWorkshop['machineName']=$machine->machineName;

        Workshop::where('id',$id)->update($updateWorkshop);
        return response()->json('修改成功');
    }






}
